<template>
  <div>
    <el-card class="box-card" shadow="always">
      <div class="clearfix" slot="header">
        <b>
          <svg-icon icon-class="笑脸" />
          角色列表</b>
      </div>
      <el-form
        ref="queryForm"
        :model="queryParams"
        size="mini"
        :inline="true"
      >
        <el-form-item>
          <el-button
              type="success"
              icon="el-icon-plus"
              @click="handleAdd"
              :disabled="isAuth('sys:role:add')">
            新增
          </el-button>
        </el-form-item>
        <el-form-item prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="角色名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain icon="el-icon-search" @click="handleQuery">搜索
          </el-button>
        </el-form-item>
      </el-form>

      <el-table
        ref="roleTable"
        v-loading="loading"
        :data="pageList"
        @row-click="handleRowClick"
        border
        highlight-current-row>
        <el-table-column width="55" align="center">
          <template slot-scope="scope">
            <el-radio v-model="checkedRoleId" :label="scope.row.roleId" :disabled="isChoose(scope.row)"><span></span></el-radio>
          </template>
        </el-table-column>

        <el-table-column label="角色名称" prop="name"/>
        <el-table-column label="角色编码" prop="code"/>
        <el-table-column label="角色备注">
          <template slot-scope="scope">
            <el-tag type="info" size="mini" effect="plain" v-if="scope.row.remark == null">暂无备注</el-tag>
             {{scope.row.remark}}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="100">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              circle
              plain
              :disabled="isAuth('sys:role:update')"
              @click.stop="handleEdit(scope.row)"
            />
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              circle
              plain
              :disabled="isAuth('sys:role:delete')"
              @click.stop="handleDelete(scope.row)"
            />
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="pagination.total>0"
        :total="pagination.total"
        :page.sync="pagination.currentPage"
        :limit.sync="pagination.pageSize"
        @pagination="handleQuery"
      />

      <!-- 角色弹窗 -->
      <el-dialog
          :title="dialog.title"
          :visible.sync="dialog.visible"
          width="450px"
          @close="closeDialog"
      >
        <el-form
            ref="form"
            :model="roleForm"
            :rules="rules"
            label-width="80px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name"></el-input>
          </el-form-item>

          <el-form-item label="角色编码" prop="code">
            <el-input v-model="roleForm.code" placeholder="请输入角色编码"/>
          </el-form-item>
          <el-form-item label="角色备注">
            <el-input v-model="roleForm.remark" placeholder="角色备注" type="textarea"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </el-dialog>

    </el-card>
  </div>
</template>

<script>
import {getRolePageList, updateRole, addRole, deleteRole,roleDetail} from '@/api/system/role'

export default {
  computed:{
    isChoose() {
      return function (row) {
        const roleId = this.$store.getters.roleId;
        return roleId.indexOf(row.roleId) != -1
      };
    }
  },
  data() {
    return {
      checkedRoleId: undefined,
      forbidden: true,
      // 遮罩层
      loading: true,
      queryParams: {
        queryMode: 'page',
        name: undefined
      },
      pagination: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      pageList: [],
      dialog: {
        title: undefined,
        visible: false
      },
      // 表单参数
      roleForm: {
      },
      // 表单校验
      rules: {
        name: [
          {required: true, message: '角色名称不能为空', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '角色编码不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    this.handleQuery()
  },
  methods: {
    handleQuery() {
      this.queryParams.currentPage = this.pagination.currentPage
      this.queryParams.pageSize = this.pagination.pageSize
      getRolePageList(this.queryParams).then(response => {
        this.pageList = response.data.records
        this.pagination.total = response.data.total
        this.pagination.currentPage = response.data.current
        this.pagination.pageSize = response.data.size
        this.loading = false
      })
    },
    handleReset() {
      this.pagination = {
        currentPage: 1,
        pageSize: 10,
        total: 0
      }
      this.queryParams.name = undefined,
        this.handleQuery()
    },
    handleAdd() {
      this.resetForm()
      this.dialog = {
        title: '新增角色',
        visible: true
      }
    },
    handleEdit(row) {
      this.resetForm()
      this.dialog = {
        title: '修改角色',
        visible: true
      }
      roleDetail(row.roleId).then(res => {
        this.roleForm = res.data
      })
    },
    handleDelete(row) {
      const ids = row.roleId
      this.$confirm(`确认删除角色名称为:${row.name}？`, '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteRole(ids).then((res) => {
          if (res.status) {
            this.$message.success('删除成功');
            this.handleQuery();
          } else {
            this.$message.error(res.msg)
          }
        })
      }).catch()
    },
    handleSubmit: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          const id = this.roleForm.roleId
          if (id != undefined) {
            updateRole(this.roleForm).then((res) => {
              if (res.status) {
                this.$message.success('修改成功');
                this.dialog.visible = false;
                this.handleQuery();
              } else {
                this.$message.error(res.msg)
              }
            })
          } else {
            addRole(this.roleForm).then(() => {
              this.$message.success('新增成功')
              this.dialog.visible = false
              this.handleQuery()
            })
          }
        }
      })
    },
    resetForm() {
      this.roleForm = {
      }
    },
    closeDialog() {
      this.resetForm()
      this.dialog = {
        title: undefined,
        visible: false
      }
    },
    handleRowClick(row) {
      //禁止修改自身角色
      this.checkedRoleId = row.roleId; // 勾选行
      const currentRow = JSON.parse(JSON.stringify(row));
      this.$emit('roleClick', currentRow)
    }
  }
}
</script>
